#include "dummysequenser.h"
#include "free-cube/aiclock.h"

using namespace fc;

DummySequenser::DummySequenser() :
  current_tempo_(fc::k_default_tempo),
  samplerate_(44100),
  size_to_process_(0),
  is_cycling_(true),
  is_playing_(false)
{
  Q_ASSERT (k_n_locators >= 2);
  for (int i = 0; i < k_n_locators ; ++i){
      locators_.append(Time());
    }
  locators_[0].frames = 0;
  locators_[1].frames = 50000;

}

void DummySequenser::set_samplerate(int samplerate)
{
  samplerate_ = samplerate;
}

fc::Tempo DummySequenser::current_tempo()
{
  return current_tempo_;
}

const TimeSignature & DummySequenser::signature()
{
  return signature_;
}

void DummySequenser::set_clock(QSharedPointer<AIClock> clock)
{
  clock_ = clock;
  clock_->set(0);
}

void DummySequenser::set_size_to_process(TimeT total_size) {
  size_to_process_ = total_size;

}

TimeT DummySequenser::process_frames() {
  TimeT l = locators_[0].frames;
  TimeT r = locators_[1].frames;
  if (!is_playing_) return 0;
  TimeT frames = size_to_process_;
  if (is_cycling_) {
      if (clock_->time() == r) clock_->set(l);
      if ( clock_->time() < r  &&
           (clock_->time() + size_to_process_ ) >= r) {
          frames = r - clock_->time();
        }
    }
  size_to_process_ -= frames;
  return frames;
}

TimeT DummySequenser::current_time() {
  return clock_->time();
}


bool DummySequenser::is_cycling(){ return is_cycling_; }
void DummySequenser::set_cycling(bool is_cycling){ is_cycling_ = is_cycling ;}
bool DummySequenser::is_playing(){ return is_playing_; }
void DummySequenser::set_playing(bool is_playing){ is_playing_ = is_playing; }

void DummySequenser::jump_to_locator(int n){
  if ( locators_.count() <= n ) return;
  jump_to_frame(locators_.at(n).frames);
}
void DummySequenser::jump_to_frame(TimeT frame_n){
  clock_->set(frame_n);
}

void DummySequenser::set_locator(int n, TimeT frame_n){
  if ( locators_.count() <= n ) return;
  locators_[n].frames = frame_n;
}
